﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Hardanger.Model;
using Hardanger.Features.ServerFuzzer;
using Hardanger.View;
using System.Windows;

namespace Hardanger.ViewModel
{
    public class ResultsViewViewModel : BaseViewModel
    {
        #region Fields

        private RelayCommand _showDetailsCommand;
        private RelayCommand _clearAllResultsCommand;
        private RelayCommand _removeCurrentResultCommand;

        #endregion //Fields

        #region Properties

        public ServerFuzzerResult CurrentResult { get; set; }
        public RelayCommand ShowDetailsCommand
        {
            get 
            {
                if (_showDetailsCommand == null)
                    _showDetailsCommand = new RelayCommand(r => DisplayDetails());
                return _showDetailsCommand;
            }
        }
        public RelayCommand ClearAllResultsCommand
        {
            get
            {
                if (_clearAllResultsCommand == null)
                    _clearAllResultsCommand = new RelayCommand(r => ClearAllResults());
                return _clearAllResultsCommand;
            }
        }

        public RelayCommand RemoveCurrentResultCommand
        {
            get
            {
                if (_removeCurrentResultCommand == null)
                    _removeCurrentResultCommand = new RelayCommand(r => RemoveCurrentResult());
                return _removeCurrentResultCommand;
            }
        }

        public ObservableCollection<FeatureResult> Results
        {
            get { return HardangerViewModel.Current.Results; }
        }

        #endregion //Properties

        #region Private Functions

        private void DisplayDetails()
        {
            var rd = new ResultsDetails { DataContext = CurrentResult };
            rd.Show();
        }

        private void ClearAllResults()
        {
            var mr = MessageBox.Show("Are you sure you want to clear all results?","Clear All",MessageBoxButton.YesNo);
            if (mr == MessageBoxResult.No)
                return;
            ServerFuzzerMasterEngine.ResultCount -= HardangerViewModel.Current.Results.Count;
            HardangerViewModel.Current.Results.Clear();
            ServerFuzzerMasterEngine.Current.CurrentSessionResultCount = 0;
        }

        private void RemoveCurrentResult()
        {
            var mr = MessageBox.Show("Are you sure you want toe remove this result?","Remove",MessageBoxButton.YesNo);
            if (mr == MessageBoxResult.No)
                return;

            HardangerViewModel.Current.Results.Remove(CurrentResult);
            ServerFuzzerMasterEngine.Current.CurrentSessionResultCount--;
            ServerFuzzerMasterEngine.ResultCount--;
        }

        #endregion //Private Functions
    }
}
